package com.doublecheck.project.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.editor.client.Editor.Path;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.chart.client.chart.Chart;
import com.sencha.gxt.chart.client.chart.axis.NumericAxis;
import com.sencha.gxt.chart.client.chart.event.SeriesSelectionEvent;
import com.sencha.gxt.chart.client.chart.event.SeriesSelectionEvent.SeriesSelectionHandler;
import com.sencha.gxt.chart.client.chart.series.Primitives;
import com.sencha.gxt.chart.client.chart.series.ScatterSeries;
import com.sencha.gxt.chart.client.draw.Gradient;
import com.sencha.gxt.chart.client.draw.RGB;
import com.sencha.gxt.chart.client.draw.sprite.Sprite;
import com.sencha.gxt.chart.client.draw.sprite.TextSprite;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.PropertyAccess;
import com.sencha.gxt.fx.client.Draggable;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.Resizable;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.Resizable.Dir;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.CollapseEvent;
import com.sencha.gxt.widget.core.client.event.ExpandEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.CollapseEvent.CollapseHandler;
import com.sencha.gxt.widget.core.client.event.ExpandEvent.ExpandHandler;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;

public class ScatterPlot extends Chart<Item> {
	
	private ArrayList<Item> data;
	private String title;
	private Window window;
	private ContentPanel panel;
	private VerticalLayoutContainer layout;

	public ScatterPlot(ScatterPlot_Dataset dataset, String title) {
		this.data = dataset.getDataset();
		this.title = title;
	}

	public interface DataPropertyAccess extends PropertyAccess<Item> {
		ValueProvider<Item, Double> value0();
		ValueProvider<Item, String> category();

		@Path("category")
		ModelKeyProvider<Item> categoryKey();
	}

	private static final DataPropertyAccess dataAccess = GWT.create(DataPropertyAccess.class);

	@Override
	public Widget asWidget() {
		final ListStore<Item> store = new ListStore<Item>(dataAccess.categoryKey());
		final Chart<Item> chart = new Chart<Item>();
		Gradient BGGradient = new Gradient("backgroundGradient", 480);
		store.addAll(data);
		chart.setStore(store);
		chart.setShadow(true);
		
		RGB red= new RGB("#ff0000");
		RGB yellow= new RGB("#ffff00");
		RGB green= new RGB("#00ff00");
		
		BGGradient.addStop(30, green);
		BGGradient.addStop(40, yellow);
		BGGradient.addStop(60, yellow);
		BGGradient.addStop(100, red);
		chart.addGradient(BGGradient);
		chart.setBackground(BGGradient);
		
		NumericAxis<Item> axis = new NumericAxis<Item>();
		axis.setPosition(Position.LEFT);
		axis.addField(dataAccess.value0());
		axis.setMinimum(0);
		axis.setMaximum(100);
		TextSprite title = new TextSprite(this.title);
		title.setFontSize(18);
		axis.setTitleConfig(title);
		chart.addAxis(axis);

		final ScatterSeries<Item> scatter = new ScatterSeries<Item>();
		scatter.setYAxisPosition(Position.LEFT);
		scatter.setYField(dataAccess.value0());
		Sprite marker = Primitives.circle(0, 0, 8);
		marker.setFill(new RGB(2, 93, 140));
		scatter.setMarkerConfig(marker);
		scatter.setHighlighting(true);
		chart.addSeries(scatter);
		scatter.addSeriesSelectionHandler(new SeriesSelectionHandler<Item>() {
			@Override
			public void onSeriesSelection(SeriesSelectionEvent<Item> event) {
				/* Fire corresponding action along with series selection */
				MessageBox pop = new MessageBox("Alert", event.getItem()
						.getCategory()
						+ ":"
						+ String.valueOf(event.getItem().getValue0()));
				RootPanel.get().add(pop);
				pop.show();
			}
		});
		panel = new FramedPanel();
		panel.getElement().getStyle().setMargin(10, Unit.PX);
		panel.setCollapsible(true);
		panel.setHeadingText("Scatter Plot Chart");
		panel.setPixelSize(400, 320);
		panel.setBodyBorder(true);

		/* Enable resizing */
		final Resizable resize = new Resizable(panel, Dir.E, Dir.SE, Dir.S);
		resize.setMinHeight(400);
		resize.setMinWidth(400);
		new Draggable(panel, panel.getHeader()).setUseProxy(false);

		panel.addExpandHandler(new ExpandHandler() {
			@Override
			public void onExpand(ExpandEvent event) {
				resize.setEnabled(true);
			}
		});
		panel.addCollapseHandler(new CollapseHandler() {
			@Override
			public void onCollapse(CollapseEvent event) {
				resize.setEnabled(false);
			}
		});
		
		chart.setLayoutData(new VerticalLayoutData(1, 1));
		layout = new VerticalLayoutContainer();
		layout.add(chart);
		layout.setLayoutData(new VerticalLayoutData(1, 1));
		initiateWindow();
		return panel;
	}
	
	/*For zoom-in event of the chart*/
	public void initiateWindow(){
		final VerticalLayoutContainer panelLayout = new VerticalLayoutContainer();
		final ToolBar toolBar = new ToolBar();		
		window = new Window();
	    window.setPixelSize(900, 700);
	    window.setModal(true);
	    window.setBlinkModal(true);	    
	    window.setHeadingText("Scatter Plot Chart");
	    window.addHideHandler(new HideHandler() {
	        @Override
	        public void onHide(HideEvent event) {
	            TextButton open = ((Window) event.getSource()).getData("open");
	            open.focus();
	            panelLayout.clear();
	            panelLayout.add(toolBar);
	            panelLayout.add(layout);
				panel.add( panelLayout);
	        }
	    });
	    /*Close button at the bottom of window*/
		TextButton closeBtn = new TextButton("Close");
		closeBtn.addSelectHandler(new SelectHandler() {
			@Override
			public void onSelect(SelectEvent event) {
				panelLayout.clear();
		        panelLayout.add(toolBar);
		        panelLayout.add(layout);
				panel.add(panelLayout);
				window.hide();
			}
		});
		window.addButton(closeBtn);
		window.setFocusWidget(window.getButtonBar().getWidget(0));
		/*zoom-in button on toolbar*/	    
	    TextButton openBtn = new TextButton("Zoom In");
	    openBtn.addSelectHandler(new SelectHandler() {
	    	@Override
	    	public void onSelect(SelectEvent event) {
	    		window.add(layout);
	    		window.show();
	    	}
	    });
	    window.setData("open", openBtn);	    	    
	    toolBar.add(openBtn);
	    toolBar.setLayoutData(new VerticalLayoutData(1, -1));
	    	    
        panelLayout.add(toolBar);
        panelLayout.add(layout);	
		panel.add(panelLayout);
	}
}
